home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / customcontroldemo / filetextbox.vb < prev    next >
Encoding:
Text File  |  2002-03-19  |  10.2 KB  |  294 lines

  1. Imports System.ComponentModel
  2.  
  3. ' a textbox control that lets you open the FileOpen dialog
  4.  
  5. '<ToolboxBitmap("E:\Program Files\Microsoft Visual Studio .NET\Common7\Graphics\icons\Computer\Disk08.ico")> _
  6. Public Class FileTextBox
  7.     Inherits System.Windows.Forms.UserControl
  8.  
  9. #Region " Windows Form Designer generated code "
  10.  
  11.     Public Sub New()
  12.         MyBase.New()
  13.  
  14.         'This call is required by the Windows Form Designer.
  15.         InitializeComponent()
  16.  
  17.         'Add any initialization after the InitializeComponent() call
  18.  
  19.     End Sub
  20.  
  21.     'UserControl overrides dispose to clean up the component list.
  22.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  23.         If disposing Then
  24.             If Not (components Is Nothing) Then
  25.                 components.Dispose()
  26.             End If
  27.         End If
  28.         MyBase.Dispose(disposing)
  29.     End Sub
  30.     Friend WithEvents txtFilename As System.Windows.Forms.TextBox
  31.     Friend WithEvents btnBrowse As System.Windows.Forms.Button
  32.     Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
  33.  
  34.     'Required by the Windows Form Designer
  35.     Private components As System.ComponentModel.Container
  36.  
  37.     'NOTE: The following procedure is required by the Windows Form Designer
  38.     'It can be modified using the Windows Form Designer.  
  39.     'Do not modify it using the code editor.
  40.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  41.         Me.btnBrowse = New System.Windows.Forms.Button()
  42.         Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
  43.         Me.txtFilename = New System.Windows.Forms.TextBox()
  44.         Me.SuspendLayout()
  45.         '
  46.         'btnBrowse
  47.         '
  48.         Me.btnBrowse.Location = New System.Drawing.Point(256, 8)
  49.         Me.btnBrowse.Name = "btnBrowse"
  50.         Me.btnBrowse.Size = New System.Drawing.Size(24, 24)
  51.         Me.btnBrowse.TabIndex = 1
  52.         Me.btnBrowse.Text = "..."
  53.         '
  54.         'OpenFileDialog1
  55.         '
  56.         Me.OpenFileDialog1.Filter = "All Files (*.*)|*.*"
  57.         '
  58.         'txtFilename
  59.         '
  60.         Me.txtFilename.Location = New System.Drawing.Point(8, 8)
  61.         Me.txtFilename.Name = "txtFilename"
  62.         Me.txtFilename.Size = New System.Drawing.Size(248, 20)
  63.         Me.txtFilename.TabIndex = 0
  64.         Me.txtFilename.Text = ""
  65.         '
  66.         'FileTextBox
  67.         '
  68.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnBrowse, Me.txtFilename})
  69.         Me.Name = "FileTextBox"
  70.         Me.Size = New System.Drawing.Size(296, 40)
  71.         Me.ResumeLayout(False)
  72.  
  73.     End Sub
  74.  
  75. #End Region
  76.  
  77.     '----------------------------------------------------------------
  78.     ' The Filename property
  79.     ' (a wrapper on the txtFilename.Text property)
  80.     '----------------------------------------------------------------
  81.  
  82.     <Description("The filename as it appears in the textbox")> _
  83.     Property Filename() As String
  84.         Get
  85.             Return Me.txtFilename.Text
  86.         End Get
  87.         Set(ByVal Value As String)
  88.             Me.txtFilename.Text = Value
  89.         End Set
  90.     End Property
  91.  
  92.     '----------------------------------------------------------------
  93.     ' The Filter property
  94.     ' (a wrapper on the OpenFileDialog property with same name)
  95.     '----------------------------------------------------------------
  96.  
  97.     <Description("The list of file filters"), DefaultValue("All files (*.*)|*.*")> _
  98.     Property Filter() As String
  99.         Get
  100.             Return OpenFileDialog1.Filter
  101.         End Get
  102.         Set(ByVal Value As String)
  103.             OpenFileDialog1.Filter = Value
  104.         End Set
  105.     End Property
  106.  
  107.     '----------------------------------------------------------------
  108.     ' The Filter property
  109.     ' (a wrapper on the OpenFileDialog property with same name)
  110.     '----------------------------------------------------------------
  111.  
  112.     Property FilterIndex() As Integer
  113.         Get
  114.             Return OpenFileDialog1.FilterIndex
  115.         End Get
  116.         Set(ByVal Value As Integer)
  117.             OpenFileDialog1.FilterIndex = Value
  118.         End Set
  119.     End Property
  120.  
  121.     '----------------------------------------------------------------
  122.     ' The ForeColor property
  123.     ' (a wrapper on the TextBox property with same name)
  124.     '----------------------------------------------------------------
  125.  
  126.     Shadows Property ForeColor() As Color
  127.         Get
  128.             Return txtFilename.ForeColor
  129.         End Get
  130.         Set(ByVal Value As Color)
  131.             txtFilename.ForeColor = Value
  132.         End Set
  133.     End Property
  134.  
  135.     ' Resetxxx and ShouldSerializexxxx methods
  136.  
  137.     Shadows Sub ResetForeColor()
  138.         Me.ForeColor = SystemColors.ControlText
  139.     End Sub
  140.  
  141.     Function ShouldSerializeForeColor() As Boolean
  142.         Return Not Me.ForeColor.Equals(SystemColors.ControlText)
  143.     End Function
  144.  
  145.     '----------------------------------------------------------------
  146.     ' The BackColor property
  147.     ' (a wrapper on the TextBox property with same name)
  148.     '----------------------------------------------------------------
  149.  
  150.     Shadows Property BackColor() As Color
  151.         Get
  152.             Return txtFilename.BackColor
  153.         End Get
  154.         Set(ByVal Value As Color)
  155.             txtFilename.BackColor = Value
  156.         End Set
  157.     End Property
  158.  
  159.     ' Resetxxx and ShouldSerializexxxx methods
  160.  
  161.     Shadows Sub ResetBackColor()
  162.         Me.BackColor = SystemColors.Window
  163.     End Sub
  164.  
  165.     Function ShouldSerializeBackColor() As Boolean
  166.         Return Not Me.BackColor.Equals(SystemColors.Window)
  167.     End Function
  168.  
  169.     '----------------------------------------------------------------
  170.     ' The ShowDialog method
  171.     ' (a wrapper on the OpenFileDialog method with same name)
  172.     '----------------------------------------------------------------
  173.  
  174.     Function ShowDialog() As DialogResult
  175.         ' show the OpenFile dialog, return the result
  176.         ShowDialog = OpenFileDialog1.ShowDialog
  177.         ' If the result is ok, assign the filename to the TextBox
  178.         If ShowDialog = DialogResult.OK Then
  179.             txtFilename.Text = OpenFileDialog1.FileName
  180.         End If
  181.     End Function
  182.  
  183.     ' open the FileOpen dialog when the browse button is clicked
  184.  
  185.     Private Sub btnBrowse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
  186.         ShowDialog()
  187.     End Sub
  188.  
  189.     ' Expose the TextChanged event to the outside, under another name
  190.     Event FilenameChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  191.  
  192.     Private Sub txtFilename_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFilename.TextChanged
  193.         RaiseEvent FilenameChanged(Me, e)
  194.     End Sub
  195.  
  196.     ' Expose the two OpenFileDialog events to the outside
  197.  
  198.     Event FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
  199.     Event HelpRequest(ByVal sender As Object, ByVal e As System.EventArgs)
  200.  
  201.     Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
  202.         OnFileOk(e)
  203.     End Sub
  204.  
  205.     Private Sub OpenFileDialog1_HelpRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenFileDialog1.HelpRequest
  206.         OnHelpRequest(e)
  207.     End Sub
  208.  
  209.     Protected Overridable Sub OnFileOk(ByVal e As System.ComponentModel.CancelEventArgs)
  210.         RaiseEvent FileOk(Me, e)
  211.     End Sub
  212.  
  213.     Protected Overridable Sub OnHelpRequest(ByVal e As EventArgs)
  214.         RaiseEvent HelpRequest(Me, e)
  215.     End Sub
  216.  
  217.  
  218.     Private Sub FileTextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
  219.         RedrawControls()
  220.     End Sub
  221.  
  222.     Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
  223.         ' Let the base control update the TextBox control.
  224.         MyBase.OnFontChanged(e)
  225.         ' Now we can redraw controls if necessary.
  226.         RedrawControls()
  227.     End Sub
  228.  
  229.     ' correctly resize child controls
  230.  
  231.     Private Sub RedrawControls()
  232.         ' This is the width of the control.
  233.         Dim width As Integer = Me.ClientRectangle.Width
  234.         ' This is the (desired) height of the control.
  235.         Dim btnSide As Integer = txtFilename.Height
  236.  
  237.         ' Adjust the height of the UserControl if necessary.
  238.         If Me.ClientRectangle.Height <> btnSide Then
  239.             ' Resize the UserControl.
  240.             Me.SetClientSizeCore(Me.ClientRectangle.Width, btnSide)
  241.             ' the above statement fires a nested Resize event, so exit right now.
  242.             Exit Sub
  243.         End If
  244.  
  245.         ' Resize the constituent controls.
  246.         txtFilename.SetBounds(0, 0, width - btnSide, btnSide)
  247.         btnBrowse.SetBounds(width - btnSide, 0, btnSide, btnSide)
  248.     End Sub
  249.  
  250.     '----------------------------------------------------------------
  251.     ' Override properties in order to hide them in the Property window
  252.     '----------------------------------------------------------------
  253.  
  254.     <Browsable(False)> _
  255.     Shadows Property AutoScroll() As Boolean
  256.         Get
  257.             If Not Me.DesignMode Then
  258.                 Throw New NotImplementedException()
  259.             End If
  260.         End Get
  261.         Set(ByVal Value As Boolean)
  262.             If Not Me.DesignMode Then
  263.                 Throw New NotImplementedException()
  264.             End If
  265.         End Set
  266.     End Property
  267.  
  268.     ' ensure that all controls point to the same context menu
  269.  
  270.     Overrides Property ContextMenu() As ContextMenu
  271.         Get
  272.             Return MyBase.ContextMenu
  273.         End Get
  274.         Set(ByVal Value As ContextMenu)
  275.             MyBase.ContextMenu = Value
  276.             ' Propagate the new value to constituent controls.
  277.             Dim ctrl As Control
  278.             For Each ctrl In Me.Controls
  279.                 ctrl.ContextMenu = Me.ContextMenu
  280.             Next
  281.         End Set
  282.     End Property
  283.  
  284.     ' change the style of the button when the Enabled property changes
  285.  
  286.     Private Sub FileTextBox_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.EnabledChanged
  287.         If Me.Enabled Then
  288.             btnBrowse.FlatStyle = FlatStyle.Standard
  289.         Else
  290.             btnBrowse.FlatStyle = FlatStyle.Flat
  291.         End If
  292.     End Sub
  293. End Class
  294.